package com.qriver.modules.filter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ServerWebExchange;

import com.qriver.modules.feign.IUserService;

import reactor.core.publisher.Mono;

public class AccessGlobalFilter implements GlobalFilter, Ordered {
	
	@Autowired
	private IUserService userService;
	
	public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
		String token = exchange.getRequest().getQueryParams().getFirst("token");
		String result = userService.authentication(token);
		if("true".equalsIgnoreCase(result)) {
			return chain.filter(exchange);
		}else {
			System.out.println("token 过期或异常！");
            exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
            return exchange.getResponse().setComplete();
		}
	}
	
	public int getOrder() {
		return 0;
	}
}
